﻿using System.Data.Entity;
using System.Data.Objects.SqlClient;
using System.Linq;
using Pyramid.Models;
using Pyramid.Security;

namespace Pyramid.Bll
{
	public class ConsumeHelper : BllBase
	{
		public IQueryable<ConsumeRecord> Query(ConsumeRecordQueryModel model, CustomPrincipal user)
		{
			var crquery = Db.ConsumeRecords.Include(x => x.Customer).Include(x => x.ConsumeItem);
			crquery = crquery.Where(x => x.CreateUserId == user.UserId);
			if (!string.IsNullOrWhiteSpace(model.Keywords))
			{
				var key = model.Keywords.Trim();
				crquery = crquery.Where(x => x.Customer.Code.Contains(key) || x.Customer.FullName.Contains(key));
			}
			if (model.DateFrom.HasValue)
			{
				crquery = crquery.Where(x => SqlFunctions.DateDiff("d", model.DateFrom.Value, x.CreateTime) >= 0);
			}
			if (model.DateTo.HasValue)
			{
				crquery = crquery.Where(x => SqlFunctions.DateDiff("d", model.DateTo.Value, x.CreateTime) <= 0);
			}
			return crquery;
		}

		/// <summary>
		/// 当前顾客的消费记录
		/// </summary>
		public IQueryable<ConsumeRecord> MyQuery(int customerId)
		{
			return Db.ConsumeRecords.Include(x => x.ConsumeItem).Where(x => x.CustomerId == customerId);
		}
	}
}